/**
 * 
 */
package fr.upvm.lem3.vm3d.ui.swing.dataload;

import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.Paths;

import fr.upvm.lem3.vm3d.services.parser.FileType;
import fr.upvm.lem3.vm3d.services.parser.Reader;

/**
 *******************************************************************
 *Lem3DataloaderController.java
 *19.02.2012
 *@author Guy Nougouapi
 *******************************************************************
 *
 *******************************************************************
 */
public class Lem3DataloaderController {
	
	private static final String LEM3 = "lem3";
	private static final String LEM3DAT = "lem3dat";
	private Lem3DataLoader lem3dataLoader;
	private DataLoadController dataLoaderController;
	
	public Lem3DataloaderController(Lem3DataLoader dataloader, DataLoadController dataLoaderController){
		this.setLem3dataLoader(dataloader);
		this.setDataLoaderController(dataLoaderController);
	}
	
	public Lem3DataloaderController(){
		this.setLem3dataLoader(null);
	}
	/**
	 * @return the lem3dataLoader
	 */
	public Lem3DataLoader getLem3dataLoader() {
		return lem3dataLoader;
	}

	/**
	 * @param lem3dataLoader the lem3dataLoader to set
	 */
	public void setLem3dataLoader(Lem3DataLoader lem3dataLoader) {
		this.lem3dataLoader = lem3dataLoader;
	}

	/**
	 * @return the dataLoaderController
	 */
	public DataLoadController getDataLoaderController() {
		return dataLoaderController;
	}

	/**
	 * @param dataLoaderController the dataLoaderController to set
	 */
	public void setDataLoaderController(DataLoadController dataLoaderController) {
		this.dataLoaderController = dataLoaderController;
	}

	/**
	 * 
	 */
	public void selectFileToLoad(String path) {
		getDataLoaderController().selectFileToLoad(path);
		
	}

	/**
	 * This method is invoke when the user confirm a selection on a file throw a prompted fileChooser
	 * @param file
	 */
	public void handleFileSelection(File file) {
		try {
			getLem3dataLoader().getFieldFileInput().setText(file.getPath());
			getLem3dataLoader().getLabelFileType().setText(file.getName().substring(file.getName().indexOf(".") +1));
			getLem3dataLoader().getBtnStart().setEnabled(true);
			getLem3dataLoader().setInputPath(file.getCanonicalPath());
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	/**
	 * 
	 * @param file
	 */
	public void readFilefromDisk( String filePath, String type){		
		System.out.println("Read File from Disk");
		String path;
//		path = filePath.replace("\\", "/");
		path = filePath;
		System.out.println(path);
		if(type.compareToIgnoreCase(LEM3) == 0){ //then we have a ASCII file
			Reader.getInstance().parseLEM3(path.toString(),FileType.LEM3.getCode());
		}else{	
			 	if(type.compareToIgnoreCase(LEM3DAT) == 0){ // we have a binary data
			 		Reader.getInstance().parseLEM3(path.toString(),FileType.LEM3DAT.getCode());
			 		}
			}
		System.out.println("Parser end");
		
	}
	
	/**
	 * 
	 * @param uri
	 */
	public void startReadFileFromDisk(String path) {
		int index  = path.lastIndexOf(".");
		String fileType = path.substring(index+1);
		
		//first re init the file reader instance in case.
		Reader.getInstance().init();
		readFilefromDisk( path, fileType);
		
		//
		Thread[] threads = new Thread[Thread.activeCount()];
		Thread.enumerate(threads);
		for(Thread ct: threads){
			if(ct.getName().equals("CtfLoader")){
				ct.interrupt();
				System.out.println("Thread interrupted !");
				}
		}
	}

}
